#! /bin/sh -e
### BEGIN INIT INFO
# Provides:           snmpd snmptrapd
# Required-Start:     $network $remote_fs $syslog
# Required-Stop:      $network $remote_fs $syslog
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  SNMP agents
# Description:        NET SNMP (Simple Network Management Protocol) Agents
### END INIT INFO
#
# Author:    Jochen Friedrich <jochen@scram.de>
#
set -e

. /lib/lsb/init-functions

export PATH=/sbin:/usr/local/sbin:/bin:/usr/local/bin

test -x /usr/local/sbin/snmpd || exit 0
test -x /usr/sbin/snmptrapd || exit 0

# Defaults
export MIBDIRS=/usr/share/mibs/site:/usr/share/snmp/mibs:/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp:/home/fernanda/.snmp/mibs
SNMPDRUN=yes
SNMPDOPTS='-Lsd -Lf /dev/null -p /var/run/snmpd.pid'
TRAPDRUN=no
TRAPDOPTS='-Lsd -p /var/run/snmptrapd.pid'

# Reads config file (will override defaults above)
[ -r /etc/default/snmpd ] && . /etc/default/snmpd

# Cd to / before starting any daemons.
cd /

# Create compatibility link to old AgentX socket location
if [ "$SNMPDCOMPAT" = "yes" ] && [ "$1" != status ]; then
  ln -sf /var/agentx/master /var/run/agentx
fi

case "$1" in
  start)
    log_daemon_msg "Starting network management services:"
    if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
	start-stop-daemon --quiet --start --oknodo --exec /usr/local/sbin/snmpd \
	    -- $SNMPDOPTS
	log_progress_msg " snmpd"
    fi
    if [ "$TRAPDRUN" = "yes" -a -f /etc/snmp/snmptrapd.conf ]; then
	start-stop-daemon --quiet --start --oknodo --exec /usr/sbin/snmptrapd \
	    -- $TRAPDOPTS
	log_progress_msg " snmptrapd"
    fi
    ;;
  stop)
    log_daemon_msg "Stopping network management services:"
    start-stop-daemon --quiet --stop --oknodo --exec /usr/local/sbin/snmpd
    log_progress_msg " snmpd"
    start-stop-daemon --quiet --stop --oknodo --exec /usr/sbin/snmptrapd
    log_progress_msg " snmptrapd"
    ;;
  restart)
    log_daemon_msg "Restarting network management services:"
    start-stop-daemon --quiet --stop --oknodo --exec /usr/local/sbin/snmpd
    start-stop-daemon --quiet --stop --oknodo --exec /usr/sbin/snmptrapd
    # Allow the daemons time to exit completely.
    sleep 2
    if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
	start-stop-daemon --quiet --start --exec /usr/local/sbin/snmpd -- $SNMPDOPTS
	log_progress_msg " snmpd"
    fi
    if [ "$TRAPDRUN" = "yes" -a -f /etc/snmp/snmptrapd.conf ]; then
	# Allow snmpd time to start up.
	sleep 1
	start-stop-daemon --quiet --start --exec /usr/sbin/snmptrapd -- $TRAPDOPTS
	log_progress_msg " snmptrapd"
    fi
    ;;
  reload|force-reload)
    log_daemon_msg "Reloading network management services:"
    if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
        start-stop-daemon --quiet --stop --signal 1 \
	    --pidfile /var/run/snmpd.pid --exec /usr/local/sbin/snmpd
	log_progress_msg " snmpd"
    fi
    ;;
  status)
    status=0
    if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
    	status_of_proc /usr/local/sbin/snmpd snmpd || status=$?
    fi
    if [ "$TRAPDRUN" = "yes" -a -f /etc/snmp/snmptrapd.conf ]; then
    	status_of_proc /usr/sbin/snmptrapd snmptrapd || status=$?
    fi
    exit $status
    ;;
  *)
    echo "Usage: /etc/init.d/snmpd {start|stop|restart|reload|force-reload|status}"
    exit 1
esac

exit 0
